package com.edvinli.controller;

import com.edvinli.entity.Clazz;
import com.edvinli.entity.ClazzQueryString;
import com.edvinli.entity.PageBean;
import com.edvinli.entity.Result;
import com.edvinli.service.ClazzService;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class ClazzController {
    @Autowired
    private ClazzService clazzService;


    /**
     * 分页查询班级
     * @param param
     * @return
     */
    @GetMapping("/clazzs")
    public Result page(ClazzQueryString param){
        log.info("分页查询信息：{},{},{},{},{}",param.getName(),param.getPage(),param.getPageSize(),
                param.getBegin(), param.getEnd());
        PageBean pageBean = clazzService.page(param);
        return Result.success(pageBean);
    }

    /**
     * 新增班级
     * @param clazz
     * @return
     */
    @PostMapping("/clazzs")
    public Result save(@RequestBody Clazz clazz){
        log.info("新增班级:{}", clazz);
        clazzService.save(clazz);
        return Result.success();
    }

    /**
     * 通过id查询回显班级信息
     * @param id
     * @return
     */
    @GetMapping("/clazzs/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("通过id回显信息:{}", id);
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    /**
     * 更改班级信息
     * @param clazz
     * @return
     */
    @PutMapping("/clazzs")
    public Result update(@RequestBody Clazz clazz){
        log.info("更改班级信息:{}", clazz);
        clazzService.update(clazz);

        return Result.success();
    }

    /**
     * 删除班级
     * @param id
     * @return
     */
    @DeleteMapping("/clazzs/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除班级:{}", id);
        clazzService.delete(id);

        return Result.success();
    }

    /**
     * 查询所有的班级信息
     * @return
     */
    @GetMapping("/clazzs/list")
    public Result getAll(){
        log.info("全部班级信息:{}");
        List<Clazz> list = clazzService.getAll();
        return Result.success(list);
    }

}
